package net.ecpt.hrm.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class HrAppraisalXlsServices {
	public static final String module = HrAppraisalXlsServices.class.getName();

	/**
	 * 导入
	 */
	public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
		Map<String, Object> result = ServiceUtil.returnSuccess();
		Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
		Delegator delegator = dctx.getDelegator();
		List<GenericValue> toBeStore = new ArrayList();
		List<String> messageList = new ArrayList<String>();
		List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
		LocalDispatcher dispatcher = dctx.getDispatcher();
		GenericValue userLogin = (GenericValue) context.get("userLogin");
		String createBy = userLogin.getString("partyId");
		try {
			int i = 0;

			for (Map<String, Object> row : sheetList) {
				i = i + 1;
				
					String id = ExcelPoiUtils.getCellString(row, 0);
					String typeId = ExcelPoiUtils.getCellString(row, 1);
					String objectiveId = ExcelPoiUtils.getCellString(row, 2);
					String appDate = ExcelPoiUtils.getCellString(row, 3);
					String appResult = ExcelPoiUtils.getCellString(row, 4);
					String appContent = ExcelPoiUtils.getCellString(row, 5);
					String remark = ExcelPoiUtils.getCellString(row, 6);
					
					// "类型,目标id,考评日期,考评结果,考评内容,备注
					

					// 判断
					if (UtilValidate.isEmpty(typeId)) {
						messageList.add("第" + (i + 1) + "行,类型不能为空。");
						continue;
					}
					
					if (UtilValidate.isEmpty(objectiveId)) {
						messageList.add("第" + (i + 1) + "行,目标id不能为空。");
						continue;
					}

					// 判断
					if (UtilValidate.isEmpty(appDate)) {
						messageList.add("第" + (i + 1) + "行,考评日期不能为空。");
						continue;
					}
					// 判断
					if (UtilValidate.isEmpty(appResult)) {
						messageList.add("第" + (i + 1) + "行,考评结果不能为空。");
						continue;
					}
					// 判断
					if (UtilValidate.isEmpty(appContent)) {
						messageList.add("第" + (i + 1) + "行,考评内容不能为空。");
						continue;
					}

					if (UtilValidate.isEmpty(remark)) {
						messageList.add("第" + (i + 1) + "行,备注不能为空。");
						continue;
					}



					// 创建
					GenericValue entity = null;
					if (UtilValidate.isNotEmpty(id)) {

						List<GenericValue> entityList = delegator.findByAnd("HrAppraisal",
								UtilMisc.toMap("id", id), null, false);
						entity = EntityUtil.getFirst(entityList);
						if (UtilValidate.isEmpty(entity)) {
							id = delegator.getNextSeqId("HrAppraisal");
							entity = delegator.makeValue("HrAppraisal", UtilMisc.toMap("id", id));
							entity.set("statusId", "0");
							entity.set("createBy", createBy);
							entity.set("createDate", nowTimestamp);
						}

					} else {
						id = delegator.getNextSeqId("HrAppraisal");
						entity = delegator.makeValue("HrAppraisal", UtilMisc.toMap("id", id));
						entity.set("statusId", "0");
						entity.set("createBy", createBy);
						entity.set("createDate", nowTimestamp);
					}

					
					 
					List<GenericValue> afterDepartmentInfo = delegator.findByAnd("HrObjective", UtilMisc.toMap("name", objectiveId),null, false);
					GenericValue afterDepartment =EntityUtil.getFirst(afterDepartmentInfo);
					String objeId = afterDepartment.getString("departmentId");
					
					
					entity.set("typeId", typeId);
					entity.set("objeId", objeId);
					entity.set("appDate", appDate);
					entity.set("appResult", appResult);
					entity.set("appContent", appContent);
					entity.set("remark", remark);
					
					toBeStore.add(entity);

				}
		
			delegator.storeAll(toBeStore);
			result.put("messageList", messageList);
		} catch (GenericEntityException e) {
			Debug.logError(e.getMessage(), module);
		}
		return result;
	}

	/**
	 * 导出
	 */

	public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
		Delegator delegator = dctx.getDelegator();
		String ids = (String) context.get("ids");
		List<String> baseIdList = FastList.newInstance();
		if (UtilValidate.isNotEmpty(ids)) {
			baseIdList = Arrays.asList(ids.split(";"));
		}
		Map<String, Object> result = ServiceUtil.returnSuccess();

		EntityCondition mainCond = null;
		List<EntityCondition> andExprs = FastList.newInstance();
		// andExprs.add(EntityCondition.makeCondition("parentProductId",
		// EntityOperator.EQUALS, "_NA_"));
		if (UtilValidate.isNotEmpty(baseIdList))
			andExprs.add(EntityCondition.makeCondition("id", EntityOperator.IN, baseIdList));
		if (andExprs.size() > 0)
			mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

		try {
			List<GenericValue> entityList = delegator.findList("HrBecomeFullMember", mainCond, null, null, null, true);

			List<Map<String, Object>> list = FastList.newInstance();
			Map<String, Object> keyMap = FastMap.newInstance();
			keyMap.put("0", "id");
			keyMap.put("1", "objectiveId");
			keyMap.put("2", "typeId");
			keyMap.put("3", "appDate");
			keyMap.put("4", "appResult");
			keyMap.put("5", "appContent");
			keyMap.put("6", "remark");
			keyMap.put("7", "createDate");
			keyMap.put("8", "createBy");
			String headers = "编号,目标id,类型,考评日期,考评结果,考评内容,备注,创建时间,创建人";
			result.put("headers", headers);

			
			for (GenericValue entity : entityList) {
				Map<String, Object> map = FastMap.newInstance();
				String id = entity.getString("id");
				String objectiveId = entity.getString("objectiveId");
				String typeId = entity.getString("typeId");
				String appResult = entity.getString("appResult");
				String appContent = entity.getString("appContent");
				String remark = entity.getString("remark");
				String createBy = entity.getString("createBy");
				Timestamp appDate = entity.getTimestamp("appDate");
				Timestamp createDate = entity.getTimestamp("createDate");

				List<GenericValue> hrDepartmentInfo = delegator.findByAnd("HrObjective",UtilMisc.toMap("objectiveId", objectiveId), null, false);
				GenericValue hrDepartment = EntityUtil.getFirst(hrDepartmentInfo);
				String objectiveId_dicText = hrDepartment.getString("name");


				map.put("id", id);
				map.put("objectiveId_dicText", objectiveId_dicText);
				map.put("typeId", typeId);
				map.put("appResult", appResult);
				map.put("appContent", appContent);
				map.put("remark", remark);
				map.put("appContent", appContent);
				
				map.put("appDate", appDate);
				

				list.add(map);
			}
			result.put("list", list);
			result.put("keyMap", keyMap);
		} catch (GenericEntityException e) {
			Debug.logError(e.getMessage(), module);
		}
		return result;
	}

}
